package com.smitish.vueblog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smitish.vueblog.common.lang.Result;
import com.smitish.vueblog.entity.Follow;
import com.smitish.vueblog.entity.Logs;
import com.smitish.vueblog.entity.User;
import com.smitish.vueblog.service.FollowService;
import com.smitish.vueblog.service.LogsService;
import com.smitish.vueblog.service.UserService;
import com.smitish.vueblog.util.ShiroUtil;
import com.smitish.vueblog.vo.CollectionBlogVO;
import com.smitish.vueblog.vo.UserFollowerVO;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * <p>
 * 关注表 前端控制器
 * </p>
 *
 * @author Smitish
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/follow")
public class FollowController {

    @Autowired
    FollowService followService;

    @Autowired
    LogsService logsService;

    @Autowired
    UserService userService;

    /**
     * @param id          用户ID
     * @param currentPage 当前页码
     * @return 返回用户ID的关注数
     */
    @GetMapping("/getFans/{id}")
    public Result getUserFansList(@PathVariable(name = "id") Long id, @RequestParam(defaultValue = "1") Integer currentPage) {
        Page<UserFollowerVO> page1 = followService.getUserFansList(new Page<>(currentPage, 7), id);
        return Result.success(page1);
    }

    /**
     * @param currentPage 当前页码
     * @return 返回列表
     */
    @RequiresAuthentication
    @GetMapping("/getList")
    public Result getUserFollowerList(@RequestParam(defaultValue = "1") Integer currentPage) {
        Long uid = ShiroUtil.getProfile().getId();
        Page<UserFollowerVO> userFollower = followService.getUserFollower(new Page<>(currentPage, 7), uid);
        return Result.success(userFollower);
    }

    /**
     * @param id 关注对象的ID
     * @return 返回请求结果
     */
    @RequiresAuthentication
    @PostMapping("/post/{id}")
    public Result postUserFollow(@PathVariable(name = "id") Long id) {
        // 提前准备好Logs信息
        Logs logs = new Logs();
        User user = userService.getById(id);
        if (user == null) return Result.fail("关注错误，没有此关注对象");
        String username = user.getUsername();
        // 1. 从ShiroUtil中获取发起请求的请求者ID
        Long uid = ShiroUtil.getProfile().getId();
        if (uid.equals(id)) return Result.fail("关注对象不能是自己");
        logs.setUserId(uid);
        // 2. 判断是否已经关注
        Follow one = followService.getOne(new QueryWrapper<Follow>().eq("from_user_id", uid).eq("to_user_id", id));
        // 2.1 关注需要取消关注
        if (one != null) {
            // 此时已经关注
            boolean b = followService.removeById(one.getFollowId());
            if (b) {
                logs.setLogContent("取消关注了\"" + username + "\"");
                boolean b1 = logsService.saveOrUpdate(logs);
                if (b1) return Result.success("取消关注成功");
            }
            return Result.fail("取消关注失败");
        } else {
            // 2.2 未关注需要发起关注
            // new Follow 实体类
            Follow follow = new Follow();
            follow.setFromUserId(Math.toIntExact(uid));
            follow.setToUserId(Math.toIntExact(id));
            boolean b = followService.saveOrUpdate(follow);
            if (b) {
                logs.setLogContent("关注了\"" + username + "\"");
                boolean b1 = logsService.saveOrUpdate(logs);
                if (b1) return Result.success("关注成功");
            }
            return Result.fail("关注失败");
        }
        // 3. post请求结果
        // 4. 向m_logs表中写入关注或取消关注的日志
    }


    /**
     * @param id 请求对象ID
     * @return 返回是否关注此人
     */
    @RequiresAuthentication
    @GetMapping("/get/{id}")
    public Result getOneFollow(@PathVariable(name = "id") Integer id) {
        // 1. 获取发起请求用户的ID
        Long uid = ShiroUtil.getProfile().getId();
        Follow follow = followService.getOne(new QueryWrapper<Follow>().eq("from_user_id", uid).eq("to_user_id", id));
        // 2. 判断该用户是否关注了 请求对象
        // 3. 返回结果
        if (follow != null) return Result.success("取消关注");
        return Result.success("关注");
    }
}
